package com.cater.talents.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.project.entity.DemandCommunicate;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.Area;
import com.cater.sys.service.impl.AreaServiceImpl;
import com.cater.talents.entity.EduExperience;
import com.cater.talents.entity.Resume;
import com.cater.talents.entity.TalentsEduExperienceResource;
import com.cater.talents.entity.TalentsResumeResource;
import com.cater.talents.entity.TalentsWorkExperienceResource;
import com.cater.talents.entity.WorkExperience;
import com.cater.talents.service.TalentsEduExperienceResourceServiceImpl;
import com.cater.talents.service.TalentsResumeResourceServiceImpl;
import com.cater.talents.service.TalentsWorkExperienceResourceServiceImpl;

/**
 * 描述：任务完成情况控制层
 * 
 * @author yong
 * @date 2018/09/29
 */
@Controller
@RequestMapping("/talentsResumeResource")
public class TalentsResumeResourceController {

	@Autowired
	private TalentsResumeResourceServiceImpl service;
	@Autowired
	private AreaServiceImpl areaService;// 区域
	@Autowired
	private TalentsEduExperienceResourceServiceImpl eduService;// 教育经历
	@Autowired
	private TalentsWorkExperienceResourceServiceImpl workService;// 工作经历

	/**
	 * @任务完成情况管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "talents/talentsResumeResource/list";
	}

	/**
	 * @任务完成情况管理数据分页
	 * @param query
	 * @param request
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询任务完成情况分页数据")
	public PageResult<TalentsResumeResource> listData(CommonQuery query, HttpServletRequest request) throws ParseException {
		PageResult<TalentsResumeResource> result = service.findPageResult(query);
		for (TalentsResumeResource resume : result.getRows()) {
			String cellphone = resume.getCellphone();
			String lifephone = resume.getLifephone();
			String temp = "";
			if (StringUtils.isNotEmpty(cellphone)) {
				temp += cellphone;
			} else if (StringUtils.isNotEmpty(lifephone)) {
				temp += lifephone;
			} else {
				temp = "暂无";
			}
			resume.setPhoneview(temp);
			String birthdate = resume.getBirthdate();
			if (StringUtils.isNotEmpty(birthdate)) {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				Date bithday = format.parse(birthdate);
				int age = getAgeByBirth(bithday);
				resume.setAge(age + "岁");
			} else {
				resume.setAge("无");
			}
		}
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("talentsResumeResource:add")
	public String addView() {
		return "talents/talentsResumeResource/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("talentsResumeResource:add")
	@ResponseBody
	@SystemControllerLog(description = "添加任务完成情况")
	public JSONObject add(TalentsResumeResource t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("talentsResumeResource:edit")
	public String editView(String id, Model model, CommonQuery query) {
		TalentsResumeResource t = service.getById(id);
		String locationA = t.getLocation();
		if (StringUtils.isNotEmpty(locationA)) {
			Area locationarea = this.areaService.getById(locationA);
			t.setLocationArea(locationarea);
		}
		String householdA = t.getHousehold();
		if (StringUtils.isNotEmpty(householdA)) {
			Area household_area = this.areaService.getById(householdA);
			t.setHouseholdArea(household_area);
		}
		String nativesA = t.getNatives();
		if (StringUtils.isNotEmpty(nativesA)) {
			Area natives_area = this.areaService.getById(nativesA);
			t.setNativeArea(natives_area);
		}
		model.addAttribute("resume", t);
		query.setTalentsId(id);
		List<TalentsWorkExperienceResource> works = workService.getListByTalentsId(query);
		model.addAttribute("workExperiences", works);
		query.setType("1");
		List<TalentsEduExperienceResource> edus = eduService.getListByTalentsId(query);
		model.addAttribute("schooledus", edus);
		return "talents/talentsResumeResource/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("talentsResumeResource:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改任务完成情况")
	public JSONObject update(TalentsResumeResource t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			TalentsResumeResource tresume = service.getById(t.getId());
			tresume.setStatus(1);// 已采纳
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("talentsResumeResource:delete")
	@SystemControllerLog(description = "删除任务完成情况")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("talentsResumeResource:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		TalentsResumeResource t = service.getById(id);
		String locationA = t.getLocation();
		if (StringUtils.isNotEmpty(locationA)) {
			Area locationarea = this.areaService.getById(locationA);
			t.setLocationArea(locationarea);
		}
		String householdA = t.getHousehold();
		if (StringUtils.isNotEmpty(householdA)) {
			Area household_area = this.areaService.getById(householdA);
			t.setHouseholdArea(household_area);
		}
		String nativesA = t.getNatives();
		if (StringUtils.isNotEmpty(nativesA)) {
			Area natives_area = this.areaService.getById(nativesA);
			t.setNativeArea(natives_area);
		}
		String cellphone = t.getCellphone();
		String lifephone = t.getLifephone();
		String temp = "";
		if (StringUtils.isNotEmpty(cellphone)) {
			temp += cellphone;
		} else if (StringUtils.isNotEmpty(lifephone)) {
			temp += lifephone;
		} else {
			temp = "暂无";
		}
		t.setPhoneview(temp);
		model.addAttribute("resume", t);
		query.setTalentsId(id);
		query.setType("");
		List<TalentsWorkExperienceResource> workExperiences = workService.getListByTalentsId(query);
		model.addAttribute("workExperiences", workExperiences);
		model.addAttribute("workcount", workExperiences.size());
		query.setType("1");
		List<TalentsEduExperienceResource> schools = eduService.getListByTalentsId(query);
		model.addAttribute("schools", schools);
		model.addAttribute("schoolcount", schools.size());
		query.setType("2");
		model.addAttribute("resume", t);
		return "talents/talentsResumeResource/detail";
	}
	private static int getAgeByBirth(Date birthday) {
		int age = 0;
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(new Date());// 当前时间

			Calendar birth = Calendar.getInstance();
			birth.setTime(birthday);

			if (birth.after(now)) {// 如果传入的时间，在当前时间的后面，返回0岁
				age = 0;
			} else {
				age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
				if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
					age += 1;
				}
			}
			return age;
		} catch (Exception e) {// 兼容性更强,异常后返回数据
			return 0;
		}
	}

}